<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索开始-->
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-block" style="text-align: center;">
                                <button type="submit" class="layui-btn layui-icon-search" lay-submit
                                        lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                                </button>
                                <button type="submit" class="layui-btn layui-btn-warm" lay-submit
                                        onclick="$('#form')[0].reset()" lay-filter="data-search-btn"><i
                                        class="layui-icon"></i> 重 置
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--搜索结束-->

        <!--表格开始-->
        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        <!--表格结束-->

        <!--工具bar 和 字段过滤excel导出一起-->
        <script type="text/html" id="operateToolBar">
            <a class="layui-btn layui-btn-sm" lay-event="add">添加</a>
        </script>
        <!--操作的工具bar-->
        <script type="text/html" id="operateTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="show">授权</a>
        </script>

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataForm" id="dataForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id"/>
                        <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入标题"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入角色备注" name="remark" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label
                                class="layui-icon layui-icon-release"></label>提交</a>
                        <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataForm')[0].reset()"
                           lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div>
        <!--添加和修改的弹出层结束-->

        <!--选择菜单和权限的弹出层开始-->
        <div id="selectMenuDiv" style="display: none;padding: 0.3125rem;">
            <table class="layui-hide" id="menuTable" lay-filter="menuTable"></table>
        </div>
        <!--选择菜单和权限的弹出层结束-->
    </div>
</div>

<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../lib/common/api.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'table', 'layer', 'treeTable'], function () {
        const form = layui.form;
        const table = layui.table;
        const layer = layui.layer;
        const treeTable = layui.treeTable;

        const tableIns = table.render({
            elem: '#dataTable',
            url: roleInfo,
            toolbar: "#operateToolBar",
            cellMinWidth: true,
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: 'id', title: 'ID', align: "center"},
                {field: 'name', title: '角色名称', align: "center"},
                {field: 'remark', title: '备注', align: "center"},
                {title: '操作', templet: '#operateTableBar', fixed: "right", align: "center"}
            ]],
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function () {
            //执行搜索重载
            table.reload('dataTable', {
                page: {
                    curr: 1
                },
                where: $("#form").serializeObject()
            });
            return false;
        });

        //监听表头的事件
        table.on('toolbar(dataTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddNotice();
                    break;
            }
        });

        //监听行数据的事件
        table.on('tool(dataTable)', function (obj) {
            const data = obj.data; //获得当前行数据
            if (obj.event === 'delete') {
                layer.confirm('真的删除这条数据么', function (index) {
                    $.ajax({
                        url: roleInfo + "/" + data.id,
                        type: 'delete',
                        success: function (res) {
                            layer.msg(res.msg);
                            obj.del();
                            layer.close(index);
                        },
                        error: function (res) {
                            alert("删除错误");
                            console.log("删除错误");
                            console.log(res);
                        }
                    });
                });
            } else if (obj.event === 'update') {
                openUpdateNotice(data);
            } else if (obj.event === 'show') {
                show(data);
            }
        });

        //打开添加公告的弹层
        var mainIndex;
        var url;  //动态的url
        function openAddNotice() {
            mainIndex = layer.open({
                type: 1,
                title: '添加',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function () {
                    //重置表单
                    $("#dataForm")[0].reset();
                }
            });
        }

        function openUpdateNotice(data) {
            console.log(data);
            mainIndex = layer.open({
                type: 1,
                title: '修改',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    //装载数据表格
                    form.val("dataForm", data);
                }
            });
        }

        //提交数据
        form.on("submit(doSubmit)", function () {
            let params = $("#dataForm").serializeObject();
            $.ajax({
                url: roleInfo,
                data: JSON.stringify(params),
                type: "Post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.log(data);
                    if (data.code === 0) {
                        tableIns.reload();
                    } else {
                        layer.msg("保存失败");
                    }
                    layer.close(mainIndex);
                },
                error: function (data) {
                    layer.msg("保存错误" + data);

                }
            });
            return false;
        });


        function show(data) {
            var table_MenusIns;
            let mainIndex = layer.open({
                type: 1,
                title: '分配【' + data.name + '】角色和权限和菜单',
                content: $("#selectMenuDiv"),
                area: ['800px', '600px'],
                btn: ['<span class="layui-icon layui-icon-ok">确认分配</span>'],
                btnAlign: 'c',
                yes: function () {
                    var m = table_MenusIns.checkStatus();
                    var rid = data.id;
                    var params = [];
                    $.each(m, function (x, item) {
                        params.push({"rid": rid, "mid": item.id});
                    });
                    console.log(params);
                    $.ajax({
                        url: roleInfoAuthMenu,
                        data: JSON.stringify(params),
                        type: "Post",
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        success: function (data) {
                            if (data.code === 0) {
                                tableIns.reload();
                            } else {
                                layer.msg("保存失败");
                            }
                            layer.close(mainIndex);
                        },
                        error: function (data) {
                            layer.msg("保存错误" + data);

                        }
                    });
                },
                success: function (index) {
                    table_MenusIns = treeTable.render({
                        tree: {
                            iconIndex: 1,  // 折叠图标显示在第几列
                            idName: 'id',  // 自定义id字段的名称
                            pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                            isPidData: true,// 是否是pid形式数据
                            openName: 'open'  // 自定义默认展开的字段名
                        },
                        elem: '#menuTable',
                        cellMinWidth: true,
                        cols: [
                            {type: "checkbox"},
                            {field: 'title', title: '菜单名称'},
                            {field: 'id', title: 'ID', align: "center", hide: true},
                            {
                                field: 'type', width: 80, align: 'center', templet: function (d) {
                                    if (d.type == 1) {
                                        return '<span class="layui-badge layui-bg-gray">按钮</span>';
                                    }
                                    if (d.type == -1) {
                                        return '<span class="layui-badge layui-bg-blue">目录</span>';
                                    } else {
                                        return '<span class="layui-badge-rim">菜单</span>';
                                    }
                                }, title: '类型'
                            },
                        ],
                        reqData: function (_data, callback) {

                            // 在这里写ajax请求，通过callback方法回调数据
                            $.get(menuInfoNoPage, function (res) {
                                callback(res.data);  // 参数是数组类型
                                $.get(roleInfoGetAuthMenu + "/" + data.id, function (rs) {
                                    console.log(rs);
                               /*     table_MenusIns.setChecked(re.data);  // 设置选中数据*/
                                    var x=new Array();
                                    $.each(res.data,function(i,i_1){
                                        $.each(rs.data,function(j,j_1){
                                            if(i_1.id==j_1){
                                                if(!i_1.children){
                                                    x.push(j_1);
                                                }
                                            }
                                        })
                                    });
                                    table_MenusIns.setChecked(x);  // 设置选中数据
                                });
                            });

                        }
                    });
                }
            });
        }

    });
</script>
</body>
</html>